package com.ev.k3cloudBusiness.service.impl;

import com.alibaba.fastjson.JSON;
import com.ev.k3cloud.entity.K3CloudBillPushEntity;
import com.ev.k3cloud.enums.K3CloudBillFormId;
import com.ev.k3cloud.service.K3CloudService;
import com.ev.k3cloudBusiness.service.K3PurPoOrderService;
import com.ev.k3cloudBusiness.vo.K3PurOrderPushEnitityVo;
import com.ev.k3cloudBusiness.vo.K3PurOrderPushVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.lang.StringUtils;
@Service
public class K3PurPoOrderServiceImpl implements K3PurPoOrderService {

    @Autowired
    K3CloudService k3CloudService;

    @Override
    public String pushPurInStockByIds(K3PurOrderPushVo purPoOrderPushVo) {
        K3CloudBillPushEntity pushEntity = new K3CloudBillPushEntity();
        List<Long>idList= purPoOrderPushVo.getK3PurOrderPushEnitityVoList().stream().map(K3PurOrderPushEnitityVo::getFentryId).collect(Collectors.toList());
        String ids=StringUtils.join(idList.toArray(), ",");
        Map<String,Object> customParams=new HashMap<>();
        String Params;
        customParams.put("Type","零星入库下推");
        customParams.put("stockerId",purPoOrderPushVo.getStockerId());
        customParams.put("stockerEmpId",purPoOrderPushVo.getStockerEmpId());
        for(int i=0;i<purPoOrderPushVo.getK3PurOrderPushEnitityVoList().size();i++)
        {
            Params=purPoOrderPushVo.getK3PurOrderPushEnitityVoList().get(i).getStockId().toString()+","+purPoOrderPushVo.getK3PurOrderPushEnitityVoList().get(i).getStockLocId().toString()+","+purPoOrderPushVo.getK3PurOrderPushEnitityVoList().get(i).getQty().toString();
            customParams.put(purPoOrderPushVo.getK3PurOrderPushEnitityVoList().get(i).getFentryId().toString(),Params);
        }
        pushEntity.setEntryIds(ids);
        pushEntity.setRuleId("PUR_PurchaseOrder-STK_InStock");
        pushEntity.setCustomParams(customParams);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("formid", K3CloudBillFormId.PUR_PURCHASEORDER.getFormId());
        resultMap.put("data", pushEntity);
        return k3CloudService.push(JSON.toJSONString(resultMap));
    }
}
